package persentation.presentDepartmentDean;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.Naming;
import java.rmi.RemoteException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;

import dataservice.userDataService.UserDatabaseService;
import persentation.presentLogin.LoginFrame;
import po.userPO.DepartmentDeanPO;
import vo.userVO.DepartmentDeanVO;
import businesslogic.chooseCourseBl.ChooseCourseController;
import businesslogic.courseBl.CourseController;
import businesslogic.teachPlanBl.TeachPlanController;
import businesslogic.userBl.DepartmentDeanController;



public class DepartmentDeanFrame extends JFrame implements ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Title title;
	private JPanel buttonPanel;
	private DepartmentdeanTabbedPane contentPanel;
	private JPanel contentPane;
	private JButton btnNewButton,button,button_1,button_2,button_3,button_4,button_5;
	private InputTeachPlanPanel inputTeachPlanPanel=null;
	private PublishCoursePanel publishCoursePanel=null;
	private ModifyCoursePanel modifyCourse=null;
	private ShowCoursePanel showCourse=null;
	private ChangePasswordPanel changePasswordPanel=null;
	private DepartmentDeanVO departmentDeanPO=null;
	private ShowStudentPanel showStudentPanel=null;
	private AssignCourse assignCourse=null;
	private static ImageIcon BOTTOM=new ImageIcon("Graphics/departmentDean/bottom.png");
	private static ImageIcon SHOWSCORE=new ImageIcon("Graphics/studentGraphics/showScore.png");
	private static ImageIcon CHANGE_PASSWRORD=new ImageIcon("Graphics/studentGraphics/changePassword.png");
	private static ImageIcon CHOOSE_COURSE=new ImageIcon("Graphics/studentGraphics/chooseCourse.png");
	private static ImageIcon MYCOURSE=new ImageIcon("Graphics/studentGraphics/myCourse.png");
	private static ImageIcon DELETE_COURSE=new ImageIcon("Graphics/studentGraphics/deleteCourse.png");
	private static ImageIcon SHOW_STUDENT=new ImageIcon("Graphics/adminGraphics/student.png");
	private static ImageIcon ASSIGN_COURSE=new ImageIcon("Graphics/teacherGraphics/courseInfo.png");
	private DepartmentDeanBottomPanel deanBottomPanel=null;
	
	
 	public static void main(String[] args) {
		UIManager.put("RootPane.setupButtonVisible", false);
		BeautyEyeLNFHelper.translucencyAtFrameInactive = false;
		try
	    {
	        org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
	    }
	    catch(Exception e)
	    {
	    	e.printStackTrace();
	    }
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DepartmentDeanFrame frame = new DepartmentDeanFrame("D00025");
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public DepartmentDeanFrame(String DepartmentDeanID){
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);
		setTitle("南京大学学生选课系统");
		setSize(1100, 750);
		Toolkit toolkit=Toolkit.getDefaultToolkit();
		Dimension screen=toolkit.getScreenSize();
		int x=(int)(screen.getWidth()-this.getWidth())>>1;
		int y=(((int) ((screen.getHeight()-this.getHeight())-15)>>1));
		setLocation(x, y);
		
		
		departmentDeanPO=findDepartment(DepartmentDeanID);
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		title=new Title();
		getContentPane().add(title);
		title.setLocation(0, 0);
		
		buttonPanel=new JPanel();
		getContentPane().add(buttonPanel);
		buttonPanel.setLocation(0, 60);
		buttonPanel.setSize(200, 610);
		buttonPanel.setLayout(null);
		
		btnNewButton = new JButton("教学计划");
		btnNewButton.setIcon(CHOOSE_COURSE);
		btnNewButton.setFont(new Font("微软雅黑", Font.BOLD, 20));
		btnNewButton.setBounds(14, 35, 170, 50);
		buttonPanel.add(btnNewButton);
		btnNewButton.addActionListener(this);
		
		button = new JButton("发布课程");
		button.setIcon(SHOWSCORE);
		button.setFont(new Font("微软雅黑", Font.BOLD, 20));
		button.setBounds(17, 94, 170, 50);
		buttonPanel.add(button);
		button.addActionListener(this);
		
		button_1 = new JButton("修改课程");
		button_1.setIcon(DELETE_COURSE);
		button_1.setFont(new Font("微软雅黑", Font.BOLD, 20));
		button_1.setBounds(17, 154, 170, 50);
		buttonPanel.add(button_1);
		button_1.addActionListener(this);
		
		button_2 = new JButton("查看课程");
		button_2.setIcon(MYCOURSE);
		button_2.setFont(new Font("微软雅黑", Font.BOLD, 20));
		button_2.setBounds(14, 214, 170, 50);
		buttonPanel.add(button_2);
		button_2.addActionListener(this);
		
		button_3 = new JButton("修改密码");
		button_3.setIcon(CHANGE_PASSWRORD);
		button_3.setFont(new Font("微软雅黑", Font.BOLD, 20));
		button_3.setBounds(17, 394, 170, 50);
		buttonPanel.add(button_3);
		button_3.addActionListener(this);
		
		button_4 = new JButton("查看学生");
		button_4.setIcon(SHOW_STUDENT);
		button_4.setFont(new Font("微软雅黑", Font.BOLD, 20));
		button_4.setBounds(17, 274, 170, 50);
		buttonPanel.add(button_4);
		button_4.addActionListener(this);
		
		button_5 = new JButton("指选课程");
		button_5.setIcon(ASSIGN_COURSE);
		button_5.setFont(new Font("微软雅黑", Font.BOLD, 20));
		button_5.setBounds(17, 334, 170, 50);
		buttonPanel.add(button_5);
		button_5.addActionListener(this);
		
		JLabel picLabel=new JLabel(BOTTOM);
		picLabel.setBounds(0,30, 200, 600);;
		buttonPanel.add(picLabel);
		
		contentPanel=new DepartmentdeanTabbedPane();
		getContentPane().add(contentPanel);
		contentPanel.setLocation(200, 90);
		contentPanel.setSize(850, 690);
		contentPanel.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
		
		deanBottomPanel=new DepartmentDeanBottomPanel(departmentDeanPO);
		
		contentPanel.addBottom(deanBottomPanel);
		contentPanel.addOne("欢迎使用", deanBottomPanel);
		
		new DepartmentdeanExceptionHandler(this, new LoginFrame());
		inputTeachPlanPanel=new InputTeachPlanPanel(new TeachPlanController(),departmentDeanPO.getDepartmentId());
		publishCoursePanel=new PublishCoursePanel(departmentDeanPO.getDepartmentId(),new CourseController(departmentDeanPO.getDepartmentId()));
		publishCoursePanel.setTabbedPane(contentPanel);
		modifyCourse=new ModifyCoursePanel(new CourseController(departmentDeanPO.getId()), departmentDeanPO.getDepartmentId());
		modifyCourse.setTabbedPane(contentPanel);
		showCourse=new ShowCoursePanel(departmentDeanPO.getDepartmentId());
		showCourse.setTabbedPane(contentPanel);
		changePasswordPanel=new ChangePasswordPanel(new DepartmentDeanController(departmentDeanPO.getId()));
		showStudentPanel=new ShowStudentPanel(new ChooseCourseController(departmentDeanPO.getId()),departmentDeanPO.getDepartmentId());
		showStudentPanel.setTabbedPane(contentPanel);
		assignCourse=new AssignCourse(departmentDeanPO.getDepartmentId());
		assignCourse.setTabbedPane(contentPanel);
		
		
		this.contentPanel.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent e) {
				if (contentPanel.getSelectedComponent()==inputTeachPlanPanel){
					inputTeachPlanPanel.updateLeftTable();
					inputTeachPlanPanel.updateRightTable();
				}
				if (contentPanel.getSelectedComponent() == modifyCourse) {
					System.out.println(modifyCourse);
					modifyCourse.findAllCourse();
					
				}
				if (contentPanel.getSelectedComponent() == showCourse) {
					showCourse.findAllCourse();
					
				}
				if (contentPanel.getSelectedComponent() == showStudentPanel) {
					showStudentPanel.findAllStudent();
			
				}
				if (contentPanel.getSelectedComponent() == publishCoursePanel){
					publishCoursePanel.fill();
				}
			}
		});
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource()==btnNewButton){
				
				contentPanel.addOne("输入院系教学计划", inputTeachPlanPanel);
		} else if (e.getSource()==button){
				
				contentPanel.addOne("发布课程", publishCoursePanel);
				
		} else if (e.getSource()==button_1){
				
				contentPanel.addOne("修改课程信息", modifyCourse);
				
		} else if (e.getSource()==button_2){
				
				contentPanel.addOne("查看本院课程信息", showCourse);
				
		} else if (e.getSource()==button_3){
				
				contentPanel.addOne("修改密码", changePasswordPanel);
		} else if (e.getSource()==button_4){
				
				contentPanel.addOne("查看学生信息", showStudentPanel);
				
		} else if (e.getSource()==button_5){
				
				contentPanel.addOne("指选课程", assignCourse);
			
		}
	}
	
	private DepartmentDeanVO findDepartment(String departmentID){
		try {
			DepartmentDeanController de=new DepartmentDeanController(departmentID);
			DepartmentDeanVO d=(DepartmentDeanVO)de.getDepartmentDeanVO(departmentID);
			if (d==null){
				JOptionPane.showMessageDialog(null, "院系教务老师登录失败","提示",JOptionPane.OK_OPTION);
			}else {
				System.out.printf("院系教务老师登录成功！ ID=%s,Name=%s,depart=%s\n",d.getId(),d.getName(),d.getDepartmentId());
			}
			return d;
		} catch (RemoteException e) {
			System.out.println(e.getCause());
			System.out.println(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
}